package com.training.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.training.R;
import com.training.bean.Audio;

import java.util.ArrayList;
import java.util.List;

public class AudioAdapter extends BaseAdapter {
    private Context context;
    private List<Audio> items=new ArrayList<>();
    public AudioAdapter(Context context) {
        this.context = context;
    }
    public  void setData(List<Audio>items){
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Audio getItem(int position) {
        if (position >= items.size()){
            return null;
        }
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 这个方法用于设置Spinner当前显示的项
        if (convertView == null) {
            convertView =  LayoutInflater.from(context).inflate(R.layout.spinner_item, parent, false);
        }
        TextView audioName = convertView.findViewById(R.id.audioName);
        audioName.setText(items.get(position).name);
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView =  LayoutInflater.from(context).inflate(R.layout.spinner_dropdown_item, parent, false);
        }

        TextView audioName = convertView.findViewById(R.id.audioName);
        audioName.setText(items.get(position).name);
        return convertView;
    }
}
